<?php


require_once '../include.php';
checkLogined();
header("Content-type: text/html; charset=utf-8");
//checkLogined();
//排序
$order = @$_REQUEST['order']?$_REQUEST['order']:null;
$orderBy = $order?"order by p.".$order:null;
//查询
$keywords = @$_REQUEST['keywords']?$_REQUEST['keywords']:null;
$where = $keywords?"where p.pName like '%{$keywords}%'":null;
//得到数据库中的所有商品
$sql = "select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName from imooc_pro as p join imooc_cate as c on p.cId = c.id {$where}";
$totalRows = getResultNum($sql);
$pageSize = 8;
$page = @$_REQUEST['page']?(int)$_REQUEST['page']:1;
$totalPage = ceil($totalRows/$pageSize);
if ($page < 1 || $page == null || !is_numeric($page)){
    $page = 1;
}
if ($page >= $totalPage) $page = $totalPage;
$offset = ($page - 1)*$pageSize;
$sql = "select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName from imooc_pro as p join imooc_cate as c on p.cId = c.id {$where} {$orderBy} limit {$offset},{$pageSize}";
$rows = fetchAll($sql);
if(!$rows){
	echo "<script type='text/javascript'>alert('没有商品，请添加!');</script>";
	echo "<script type='text/javascript'>window.location='addPro.php';</script>";
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>大大电子商务后台管理系统</title>

    <!-- BOOTSTRAP STYLES-->
    <link href="styles/css/bootstrap.css" rel="stylesheet" />
    <!-- FONTAWESOME STYLES-->
    <link href="styles/css/font-awesome.css" rel="stylesheet" />
    <!--CUSTOM BASIC STYLES-->
    <link href="styles/css/basic.css" rel="stylesheet" />
    <!--CUSTOM MAIN STYLES-->
    <link href="styles/css/custom.css" rel="stylesheet" />
    <!-- GOOGLE FONTS-->
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<!--    <link rel="stylesheet" href="styles/backstage.css">-->
    <link rel="stylesheet" href="scripts/jquery-ui/css/ui-lightness/jquery-ui-1.10.4.custom.css" />
    <!-- /. FOOTER  -->
    <!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
    <!-- JQUERY SCRIPTS -->
    <script src="scripts/js/jquery-1.10.2.js"></script>
    <!-- BOOTSTRAP SCRIPTS -->
    <script src="scripts/js/bootstrap.js"></script>
    <!-- METISMENU SCRIPTS -->
    <script src="scripts/js/jquery.metisMenu.js"></script>
    <!-- CUSTOM SCRIPTS -->
    <script src="scripts/js/custom.js"></script>
    <script src="scripts/jquery-ui/js/jquery-ui-1.10.4.custom.js"></script>
    <script src="scripts/jquery-ui/js/jquery-ui-1.10.4.custom.min.js"></script>
</head>

<body>
<body>
<div id="wrapper">
    <nav class="navbar navbar-default navbar-cls-top " role="navigation" style="margin-bottom: 0">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.php">大大电子商务</a>
        </div>

        <div class="header-right">
            <a href="doAdminAction.php?act=logout" class="btn btn-danger" title="Logout"><i class="fa fa-sign-out fa-2x"></i></a>
        </div>
    </nav>
    <!-- /. NAV TOP  -->
    <nav class="navbar-default navbar-side" role="navigation">
        <div class="sidebar-collapse">
            <ul class="nav" id="main-menu">
                <li>
                    <div class="user-img-div">
                        <img src="styles/img/user.png" class="img-thumbnail" />

                        <div class="inner-text">
                            欢迎您：<?php
                            if(isset($_SESSION['adminName'])){
                                echo $_SESSION['adminName'];
                            }elseif(isset($_COOKIE['adminName'])){
                                echo $_COOKIE['adminName'];
                            }
                            ?>
                            <br />
                            <td><?php echo "今天是 " . date("Y-m-d")?>日</td>
                        </div>
                    </div>

                </li>


                <li>
                    <a class="active-menu" href="index.html"><i class="fa fa-dashboard "></i>主页</a>
                </li>
                <li>
                    <a href="#"><i class="fa fa-desktop "></i>商品管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="addPro.php"><i class="fa fa-toggle-on"></i>添加商品</a>
                        </li>
                        <li>
                            <a href="listPro.php"><i class="fa fa-bell "></i>商品列表</a>
                        </li>
                        <li>
                            <a href="listProImages.php"><i class="fa fa-bell "></i>商品图片&简介管理</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-yelp "></i>分类管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="addCate.php"><i class="fa fa-coffee"></i>添加分类</a>
                        </li>
                        <li>
                            <a href="listCate.php"><i class="fa fa-flash "></i>分类管理</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-flash "></i>订单管理</a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="listlist.php"><i class="fa fa-desktop "></i>订单查询</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-bicycle "></i>用户管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">

                        <li>
                            <a href="addUser.php"><i class="fa fa-desktop "></i>添加用户</a>
                        </li>
                        <li>
                            <a href="listUser.php"><i class="fa fa-code "></i>用户列表</a>
                        </li>


                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-bicycle "></i>管理员管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">

                        <li>
                            <a href="addAdmin.php"><i class="fa fa-desktop "></i>添加管理员</a>
                        </li>
                        <li>
                            <a href="listAdmin.php"><i class="fa fa-code "></i>管理员列表</a>
                        </li>


                    </ul>
                </li>

                <li>
                    <a href="blank.html"><i class="fa fa-square-o "></i>Blank Page</a>
                </li>
            </ul>

        </div>

    </nav>
    <!-- /. NAV SIDE  -->
    <div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <h1 class="page-head-line">管理员告示栏</h1>
                    <h1 class="page-subhead-line">这是一些通知事项 </h1>

                </div>
            </div>
            <div class="input-group"style="margin-left:auto;margin-right:2px;width:300px;">
                <input type="text" class="form-control" id="search" placeholder="Enter Keywords" />
                <span class="input-group-btn">
                                <a class="btn btn-primary" onclick="search()">
                                <i class="fa fa-gear fa-spin"></i>&nbsp;&nbsp;搜索商品
                                </a>
                            </span>
            </div>
            <div class="col-md-12">
                <!--    Context Classes  -->
                <div class="panel panel-default">

                    <div class="panel-heading">
                        商品列表
                    </div>
                    <div style="margin-left:auto;margin-right:2px;width:300px;margin-top: 10px;">
                        <div class="btn-group">
                            <button data-toggle="dropdown" class="btn btn-warning dropdown-toggle">商品价格 <span class="caret"></span></button>
                            <ul class="dropdown-menu">
                                <li><a >-请选择-</a></li>
                                <li class="divider"></li>
                                <li><a herf="#" value="iPrice asc">由低到高</a></li>
                                <li class="divider"></li>
                                <li><a href="#" value="iPrice desc">由高到低</a></li>
                            </ul>
                        </div>
                        <div class="btn-group">
                            <button data-toggle="dropdown" class="btn btn-success dropdown-toggle">上架时间 <span class="caret"></span></button>
                            <ul class="dropdown-menu">
                                <li><a >-请选择-</a></li>
                                <li class="divider"></li>
                                <li><a href="#" value="pubTime desc">最新发布</a></li>
                                <li class="divider"></li>
                                <li><a href="#" value="pubTime asc">历史发布</a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                <th width="10%">选择</th>
                                <th width="20%">商品名称</th>
                                <th width="10%">商品分类</th>
                                <th width="10%">是否上架</th>
                                <th width="15%">上架时间</th>
                                <th width="10%">大大价格</th>
                                <th>操作</th>
                                </thead>
                                <tbody>
                                <?php foreach ($rows as $row):?>
                                    <tr>
                                        <!--这里的id和for里面的c1 需要循环出来-->
                                        <td><input type="checkbox" id="c1" class="check" value=<?php echo $row['id'];?>><label for="c1" class="label"><?php echo $row['id'];?></label></td>
                                        <td><?php echo $row['pName']; ?></td>
                                        <td><?php echo $row['cName'];?></td>
                                        <td>
                                            <?php
                                            $show=($row['isShow']==1)?"上架":"下架";
                                            echo $show;
                                            ?>
                                        </td>
                                        <td><?php echo date("Y-m-d H:i:s",$row['pubTime']);?></td>
                                        <td><?php echo $row['iPrice'];?>元</td>
                                        <td align="center">
                                            <input type="button" value="详情" class="btn" onclick="showDetail(<?php echo $row['id'];?>,'<?php echo $row['pName'];?>')"><input type="button" value="修改" class="btn" onclick="editPro(<?php echo $row['id'];?>)"><input type="button" value="删除" class="btn" onclick="delPro(<?php echo $row['id'];?>)">
                                            <div id="showDetail<?php echo $row['id'];?>" style="display:none;">
                                                <table class="table" cellspacing="0" cellpadding="0">
                                                    <tr>
                                                        <td width="20%" align="right">商品名称</td>
                                                        <td><?php echo $row['pName'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">商品类别</td>
                                                        <td><?php echo $row['cName'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">商品货号</td>
                                                        <td><?php echo $row['pSn'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">商品数量</td>
                                                        <td><?php echo $row['pNum'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td  width="20%"  align="right">商品官方价格</td>
                                                        <td><?php echo $row['mPrice'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td  width="20%"  align="right">大大价格</td>
                                                        <td><?php echo $row['iPrice'];?></td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">商品图片</td>
                                                        <td>
                                                            <?php
                                                            $proImgs = getAllImgByProId($row['id']);
                                                            foreach ($proImgs as $proImg):
                                                                ?>
                                                                <img width="100" height="100" src="uploads/<?php echo $proImg['albumPath'];?>" alt="" />&nbsp;&nbsp;
                                                            <?php endforeach;?>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">是否上架</td>
                                                        <td>
                                                            <?php
                                                            $show=($row['isShow']==1)?"上架":"下架";
                                                            echo $show;
                                                            ?>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td width="20%"  align="right">是否热卖</td>
                                                        <td>
                                                            <?php
                                                            $hot=($row['isHot']==1)?"热卖":"正常";
                                                            echo $hot;
                                                            ?>
                                                        </td>
                                                    </tr>
                                                </table>
                                                <span style="display:block;width:80%; ">
                	商品描述<br/>
                                                    <?php echo $row['pDesc'];?>
                	</span>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach;?>
                                <?php if ($totalRows > $pageSize):?>
                                    <tr>
                                        <td colspan="7" align="center"><?php echo showPage($page, $totalPage,"keywords={$keywords}&$order={$order}");?></td>
                                    </tr>
                                <?php endif;?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!--  end  Context Classes  -->
            </div>
        </div>
        <!-- /. PAGE INNER  -->
    </div>
</div>
<script type="text/javascript">
    function showDetail(id,t){
        $("#showDetail"+id).dialog({
            height:"auto",
            width: "auto",
            position: {my: "center", at: "center",  collision:"fit"},
            modal:false,//是否模式对话框
            draggable:true,//是否允许拖拽
            resizable:true,//是否允许拖动
            title:"商品名称："+t,//对话框标题
            show:"slide",
            hide:"explode"
        });
    }
    function addPro(){
        window.location='addPro.php';
    }
    function editPro(id){
        window.location='editPro.php?id='+id;
    }
    function delPro(id){
        if(window.confirm("您确定要删除吗？添加一次不易，且删且珍惜！")){
            window.location='doAdminAction.php?act=delPro&id='+id;
        }
    }
    function search(){
        if(event.keyCode==13){
            var val=document.getElementById("search").value;
            window.location="listPro.php?keywords="+val;
        }
    }
    function change(val){
        window.location="listPro.php?order="+val;
    }
</script>
<div id="footer-sec">
    &copy; 2017 白尚林 | More Templates
</div>
</body>
</html>